package com.neuqsoft.ymtdemo.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.neuqsoft.ymtdemo.view.WebViewEsscH5Activity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * <pre>
 *     author : wulh
 *     e-mail : wulh@neuqsoft.com
 *     time   : 2020/12/04
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String APP_ID = "wx32efa175f67fe677";
    IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, APP_ID, false);
        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        WXLaunchMiniProgram.Req baseReq1 = (WXLaunchMiniProgram.Req) baseReq;
        if (baseReq1.extData != null) {
            Log.e("extraData", baseReq1.extData);
        }
        Toast.makeText(WXEntryActivity.this, "进入了onReq！！！", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onResp(BaseResp baseResp) {
        Toast.makeText(WXEntryActivity.this, "进入了onResp！！！", Toast.LENGTH_SHORT).show();
        if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
            String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter
             Intent intent = new Intent(WXEntryActivity.this, WebViewEsscH5Activity.class);
            if (!TextUtils.isEmpty(extraData)) {
                Log.e("extraData", extraData);
                Toast.makeText(WXEntryActivity.this, extraData, Toast.LENGTH_SHORT).show();
                intent.putExtra("extraData", extraData);
            }
                startActivity(intent);

        }
    }
}
